<?php

namespace App\Console\Commands;

use App\Models\Product;
use App\Models\ProductSeckill;
use App\Service\ConfigService;
use Illuminate\Console\Command;

class SeckillGoodsOver extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'seckill:over';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单秒杀结束自动恢复商品类型';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $seckills = ProductSeckill::with('product')
            ->whereHas('product',function ($query) {
                $query->where('type', Product::TYPE_SECKILL);
            })->where('end_at', '<', now())->get();

        if ($seckills->isNotEmpty()) {

            foreach ($seckills as $seckill) {

                foreach ($seckill['product'] as $value) {
                    $value->update([
                        'type' => Product::TYPE_NORMAL
                    ]);
                }
            }
        }
    }
}
